package com.dyzx.hbb.modules.sys.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseController;
import com.dyzx.hbb.modules.sys.entity.SysAddress;
import com.dyzx.hbb.modules.sys.entity.SysAddress;
import com.dyzx.hbb.modules.sys.query.AddressQuery;
import com.dyzx.hbb.modules.sys.service.SysAddressService;
import com.dyzx.hbb.modules.sys.vo.AddressUVO;
import com.dyzx.hbb.modules.sys.vo.AddressVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/sys/address")
@RestController
@Tag(name = "10.地址管理", description = "地址相关接口")
public class AddressController extends BaseController<SysAddressService, SysAddress, AddressQuery, AddressVO, AddressUVO> {

    @Autowired
    private SysAddressService service;



    @Override
    protected SysAddressService getBaseService() {
        return service;
    }



    @Override
    protected MyLambdaQueryWrapper<SysAddress> queryWrapper(AddressQuery query) {
        MyLambdaQueryWrapper<SysAddress> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysAddress::getId, SysAddress::getName, SysAddress::getCode, SysAddress::getOrgin, SysAddress::getParentId, SysAddress::getLevel, SysAddress::getOperatorId, SysAddress::getStatus, SysAddress::getCreateDate, SysAddress::getUpdateDate);
        if (query != null) {


            wrapper.like(StringUtils.isNotBlank(query.getName()), SysAddress::getName, query.getName())
                    .like(StringUtils.isNotBlank(query.getCode()), SysAddress::getCode, query.getCode())
                    .like(StringUtils.isNotBlank(query.getOrigin()), SysAddress::getOrgin, query.getOrigin())
                    .eq(query.getParentId()!=null, SysAddress::getParentId, query.getParentId())
                    .eq(query.getLevel()!=null, SysAddress::getLevel, query.getLevel())

                  ;

            return wrapper;
        } else {
            return wrapper;
        }

    }

    @Override
    protected MyLambdaQueryWrapper<SysAddress> queryInfoWrapper() {
        MyLambdaQueryWrapper<SysAddress> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysAddress::getId, SysAddress::getName,
                       SysAddress::getCode, SysAddress::getOrgin, SysAddress::getParentId, SysAddress::getLevel, SysAddress::getOperatorId, SysAddress::getStatus, SysAddress::getCreateDate, SysAddress::getUpdateDate);
        wrapper.userName(SysAddress::getOperatorId).as("operator_name");
           //     .addressName(SysAddress::getParentId).as("parent_name");
        return wrapper;
    }

    @Operation(summary = "地址树形查询")
    @GetMapping("/list_tree")
    public Result<List<Map<String, Object>>> listTree(
            @RequestParam(name = "key", defaultValue = "0") long key,
            @RequestParam(name = "level", defaultValue = "1") int level
    ) {


        List<Map<String, Object>> page1 = service.findByTreeList(key,level);

        return Result.ok(page1);

    }


}
